package cc.eddic.pratice.programmingparadigms.exam.impl;

import cc.eddic.pratice.programmingparadigms.emp02list.impl.ResultListGcsImpl;
import cc.eddic.pratice.programmingparadigms.exam.Model.AnswerModel;
import cc.eddic.pratice.programmingparadigms.exam.QuestionBankService;
import cc.eddic.pratice.programmingparadigms.exam.QuestionType;
import cc.eddic.pratice.programmingparadigms.exam.domain.Category;
import cc.eddic.pratice.programmingparadigms.exam.domain.Option;
import cc.eddic.pratice.programmingparadigms.exam.domain.Question;
import cc.eddic.pratice.programmingparadigms.exam.domain.SubCategory;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;


public class QuestionBankServiceLhImpl extends QuestionBankServiceImpl implements QuestionBankService {
    private static final Logger log = LoggerFactory.getLogger(ResultListGcsImpl.class);
    @Override
    public List<Question> importQuestions(String questionTextFile) {
        String file = getQuestionFileContent();
        String[] filelist = file.split("\n");
        List<Question> questionList = new ArrayList<>();
        for (int i = 0, n = 1; i < filelist.length; i++) {
            if (Objects.equals(filelist[i], "")) {
                continue;
            }
            if (Character.isDigit(filelist[i].charAt(0))) {
                Question q1 = new Question(n, filelist[i].substring(filelist[i].indexOf("、") + 1), QuestionType.SINGLE_CHOICE);
                for (int j = 0; j < 4; j++) {
                    i++;
                    q1.add(new Option(filelist[i].substring(2), filelist[i].charAt(0), false));
                }
                questionList.add(q1);
                n++;
            }
        }
        return questionList;
    }

    @Override
    public List<Question> importCorrectAnswers(String correctAnswerTextFile) {
//        导入含正确答案的  QestionList
        List<Question> questions = importQuestions(" ");
        Map<Integer, Character> answermap = readCorrectAnswers2(" ");
        for (Question qu : questions) {
            if (!answermap.containsKey(qu.getNo())) {
                continue;
            }
            qu.getOptions().stream().filter(
                            option -> (answermap.get(qu.getNo()).equals(option.getChoice())))
                    .findFirst().orElseThrow(IllegalArgumentException::new).changeToCorrectAnswer();
        }
        return questions;
    }

    @Override
    public List<AnswerModel> readCorrectAnswers(String correctAnswerTextFile) {
        String answer = "1\t2\t3\t4\t5\t6\t7\t8\t9\t10\n" +
                "B\tC\tA\tC\tD\tB\tB\tA\tD\tB\n" +
                "11\t12\t13\t14\t15\t16\t17\t18\t19\t20\n" +
                "B\tC\tA\tC\tA\tB\tD\tC\tD\tB\n" +
                "21\t22\t23\t24\t25\n" +
                "A\tD\tA\tC\tB\n";
        String[] answerlist = answer.split("\n");
        List<AnswerModel> answerList = new ArrayList<>();
        for (int i = 0; i < answerlist.length; i = i + 2) {
            String[] num1 = answerlist[i].split("\t");
            String[] num2 = answerlist[i + 1].split("\t");
            for (int j = 0; j < num1.length; j++) {
                AnswerModel a = new AnswerModel(Integer.parseInt(num1[j]), num2[j].charAt(0));
                answerList.add(a);
            }
        }
        return answerList;

    }

    @Override
    public Map<Integer, Character> readCorrectAnswers2(String correctAnswerTextFile) {
        Map<Integer, Character> map = new HashMap<>();
        String answerfile = "1\t2\t3\t4\t5\t6\t7\t8\t9\t10\n" +
                "B\tC\tA\tC\tD\tB\tB\tA\tD\tB\n" +
                "11\t12\t13\t14\t15\t16\t17\t18\t19\t20\n" +
                "B\tC\tA\tC\tA\tB\tD\tC\tD\tB\n" +
                "21\t22\t23\t24\t25\n" +
                "A\tD\tA\tC\tB\n";
        String[] answerlist = answerfile.split("\n");
        for (int i = 0; i < answerlist.length; i = i + 2) {
            String[] questionNo = answerlist[i].split("\t");
            String[] answer = answerlist[i + 1].split("\t");
            for (int j = 0; j < questionNo.length; j++) {
                map.put(Integer.parseInt(questionNo[j]), answer[j].charAt(0));
            }
        }
        return map;
    }

    @Override
    public List<Question> importCategories(String categoryExcelFile) {
        try {
            HashMap<String, String> categoryMap = new HashMap<>();
            categoryMap.put("基础", "软件开发基础");
            categoryMap.put("HTML", "软件开发基础");
            categoryMap.put("SQL", "软件开发基础");
            categoryMap.put("DOS", "软件开发基础");
            categoryMap.put("Linux", "软件开发基础");
            categoryMap.put("数学", "软件开发基础");

            categoryMap.put("Word", "办公软件");
            categoryMap.put("Excel", "办公软件");

            categoryMap.put("代码", "代码");


            FileInputStream file = new FileInputStream(categoryExcelFile);
            Workbook workbook = new XSSFWorkbook(file);

            List<Question> questionList = importCorrectAnswers(" ");

            Sheet categorySheet = workbook.getSheetAt(0);
            Iterator<Row> rowIterator = categorySheet.rowIterator();

            rowIterator.next();
            Row rowSubCategory = rowIterator.next();

            while (rowIterator.hasNext()) {
                try {
                    Row row = rowIterator.next();
                    CellType cellType = row.getCell(0).getCellType();
                    if (cellType == CellType.STRING) {
                        break;
                    }
                    int no = (int) row.getCell(0).getNumericCellValue();


                    Iterator<Cell> cellIterator = row.cellIterator();
                    while (cellIterator.hasNext()) {
                        Cell cell = cellIterator.next();
                        if (cell.getNumericCellValue() == 1) {
                            int columnIndex = cell.getColumnIndex();
                            String nameSubCategory = rowSubCategory.getCell(columnIndex).getRichStringCellValue().getString();
                            SubCategory subcategory = new SubCategory(nameSubCategory);
                            subcategory.setCategory(new Category(categoryMap.get(nameSubCategory)));
                            questionList.get(no - 1).setSubCategory(subcategory);
                            break;
                        }
                    }

                } catch (NumberFormatException e) {
                    break;
                }
            }

            return questionList;
        } catch (FileNotFoundException e) {
            log.error("importCategories FileInputStream FileNotFoundException\n");
        } catch (IOException e) {
            log.error("importCategories XSSFWorkbook IOException\n");
        }
        return null;
    }
}
